package com.corn.core.spring.ioc;

import java.util.Enumeration;

import javax.servlet.ServletContext;

import jodd.util.StringUtil;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.FrameworkServlet;
/**
 * 
 *
 * @author madong
 * @date Dec 23, 2012 4:55:54 PM
 */
public class SpringIocUtil {
	public static String fail_info = "get %s  fail,ServletContext  not loading.";
	private static final Log log=LogFactory.getLog(SpringIocUtil.class);
	public static String springConfigFilePath = "applicationContext.xml";
	public static String success_info = "get %s   success:";

	public static synchronized <T> T getBean(Class<T> beanName) {
		return getInstence(beanName, getContext());
	}

	public static synchronized <T> T getBean(Class<T> beanName,ApplicationContext context) {

		return getInstence(beanName, context);
	}

	public static synchronized <T> T getBean(Class<T> beanName,ServletContext servletcontext) {

		return getInstence(beanName, getContext());
	}

	public static synchronized Object getBean(String beanName) {
		return getInstence(beanName, getContext());
	}

	public static synchronized Object getBean(String beanName,ApplicationContext context) {
		return getInstence(beanName, context);
	}

	public static synchronized Object getBean(String beanName,ServletContext servletcontext) {
		return getInstence(beanName, getContext());
	}

	

	private static  <T> T getInstence(Class<T> beanName,ApplicationContext context) {
		try {
			if (context == null) {
				log.error(String.format(fail_info, beanName));
				return null;
			}
			T t = context.getBean(beanName);
			if (t != null) {
				log.debug(String.format(success_info, beanName) + t);
				return t;
			}
		} catch (Exception e) {
			log.error(e);
		}
		return null;
	}

	private static  Object getInstence(String beanName,ApplicationContext context) {
		try {
			if (getContext() == null) {
				log.error(String.format(fail_info, beanName));
				return null;
			}
			Object t = context.getBean(beanName);
			if (t != null) {
				log.debug(String.format(success_info, beanName) + t);
				return t;
			}
		} catch (Exception e) {
			log.error(e);
		}
		return null;
	}

	
public static ApplicationContext getContext() {
		
		ApplicationContext context = null;
		try{
			Enumeration<String> ns= SpringIocContextListener.getServletcontext().getAttributeNames();
			while(ns.hasMoreElements()){
				String name=ns.nextElement();
				System.out.println(name);
				System.out.println(name.matches(FrameworkServlet.SERVLET_CONTEXT_PREFIX+"*"));
			}
			
			
			context = WebApplicationContextUtils.getWebApplicationContext(SpringIocContextListener.getServletcontext());
		}catch(Exception e){
			log.warn("Spring IOC getWebApplicationContext(sc) type fail.");
		}
		
		String name=null;
		if (context == null) {
			
			try{
				name=FrameworkServlet.SERVLET_CONTEXT_PREFIX+"Spring-MVC";
				context=WebApplicationContextUtils.getWebApplicationContext(SpringIocContextListener.getServletcontext(), name);
			}catch(Exception e){
				log.warn("Spring IOC getWebApplicationContext(sc,\""+name+"\") type fail[default].");
			}
		}
		
		if (context == null) {
			try{
				Enumeration<String> ns_= SpringIocContextListener.getServletcontext().getAttributeNames();
				while(ns_.hasMoreElements()){
					 name=ns_.nextElement();
					if(null!=name&&name.trim().length()>0&&name.matches(FrameworkServlet.SERVLET_CONTEXT_PREFIX+"*")){
						context=WebApplicationContextUtils.getWebApplicationContext(SpringIocContextListener.getServletcontext(), name);
						break;
					}
				}
			}catch(Exception e){
				log.warn("Spring IOC getWebApplicationContext(sc,\""+name+"\") type fail.");
			}
		}
		if (context == null) {
			try{
				context=WebApplicationContextUtils.getRequiredWebApplicationContext(SpringIocContextListener.getServletcontext());
			}catch(Exception e){
				log.warn("Spring IOC getRequiredWebApplicationContext type fail.");
				
			}
		}
		if (context == null) {
			try{
		      context = new ClassPathXmlApplicationContext(springConfigFilePath);
			}catch(Exception e){
				e.printStackTrace();
				log.warn("Spring IOC ClassPathXmlApplicationContext type fail.");
				
			}
		}
		return context;
	}

	private static ApplicationContext initLocalAppContext() {
		if(StringUtil.isEmpty(springConfigFilePath)){
			log.error("spring Config FilePath is empty.");
			return null;
		}
		ApplicationContext context = new ClassPathXmlApplicationContext(
				springConfigFilePath);
		log.info("load " + springConfigFilePath + " success!");
		return context;
	}

}
